using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Geomatrix
{
    class LevelSelect
    {
        Button[] levelList = new Button[50];
        int ScrollY = 0;
        int MaxY = 0;
        Texture2D _butTex;
        Texture2D _backTex;

        //Last position
        int lastY;

        //

        public LevelSelect(Texture2D butTex, Texture2D backTex)
        {
            _butTex = butTex;
            _backTex = backTex;

            //Insert test levels:
            levelList[0] = new Button(new Vector2(36, 00), _butTex, "Return");
            levelList[1] = new Button(new Vector2(36, 32), _butTex, "Level 2");
            levelList[2] = new Button(new Vector2(36, 64), _butTex, "Level 3");
            levelList[3] = new Button(new Vector2(36, 96), _butTex, "Level 4");
            levelList[4] = new Button(new Vector2(36, 128), _butTex, "Level 5");
            levelList[5] = new Button(new Vector2(36, 160), _butTex, "Level 6");
            levelList[6] = new Button(new Vector2(36, 192), _butTex, "Level 7");
            levelList[7] = new Button(new Vector2(36, 224), _butTex, "Level 8");
            levelList[8] = new Button(new Vector2(36, 256), _butTex, "Level 9");
            levelList[9] = new Button(new Vector2(36, 288), _butTex, "Level 10");


            ScrollY = 0;
            MaxY = setMaxY();
        }

        private int setMaxY()
        {
            int ret = -440;
            for (int index = 0; index < levelList.Length; ++index)
            {
                if (levelList[index] != null)
                    ret += 48;
                else break;
            }

            return ret;
        }

        public int Update(TouchCollection tc)
        {
            //Update the button positions/scroll
            foreach(TouchLocation tl in tc)
            {
                if (tl.State == TouchLocationState.Pressed)
                    lastY = (int)tl.Position.Y;
                else
                {
                    ScrollY += lastY - (int)tl.Position.Y;
                    lastY = (int)tl.Position.Y;
                }
            }
            if (ScrollY < -40)
                ScrollY = -40;
            else if (ScrollY > MaxY)
                ScrollY = MaxY;

            for (int index = 0; index < levelList.Length; ++index)
            {
                if (levelList[index] == null)
                    continue;
                
                levelList[index].SetPosition(new Vector2(36, (index*48)+8-ScrollY));
                if (levelList[index].Update(tc))
                {
                    if (index == 0)
                        return 0;
                    else
                        return 2;
                }
            }

            return 1; //If true is returned than end game.
        }

        public void Draw(SpriteBatch sb)
        {
            sb.Draw(_backTex, new Vector2(0, 0), Color.White);

            for (int index = 0; index < levelList.Length; ++index)
            {
                if (levelList[index] == null)
                    continue;

                levelList[index].Draw(sb);
            }
        }
    }
}